#ifndef WIDGETBOX_H
#define WIDGETBOX_H

#include "qframe.h"
#include "qtreewidget.h"
#include <QWidget>
#include <QPushButton>
#include <QTreeWidget>
#include <DefineCommon.h>
#include <QLayout>
#include <QVBoxLayout>

class AbstractCategory;
class WidgetBoxFrame : public QFrame
{
public:
    virtual ~WidgetBoxFrame()
    {
        ELOG_DEBUG("delete WidgetBoxFrame" );
    };

    QVBoxLayout* m_vLayOut;
};

class WidgetBox : public QWidget
{
    Q_OBJECT
    Q_PROPERTY(int currentIndex READ currentIndex WRITE setCurrentIndex)
    Q_PROPERTY(QString pageTitle READ pageTitle WRITE setPageTitle STORED true)
    Q_PROPERTY(bool isPageExpanded READ isPageExpanded WRITE setPageExpanded)
    Q_PROPERTY(CategoryType categoryType READ categoryType WRITE setCategoryType)
    Q_ENUMS(CategoryType)

public:
    enum CategoryType {
        Button,
        Line,
        Frame   // TODO
    };

    WidgetBox(QWidget *parent = 0);
    ~WidgetBox();

    void addWidgetAndCategoryNew(QWidget* wPtr, const QString& categoryName);
    void addWidgetAndCategory(QWidget* wPtr, const QString& categoryName);
    void clear();
    /**loop all child QCombobox,QDoubleSpinBox,QSpinBox and install eventFilter to avoid mouse's wheel event*/
    void installEventFilterForChildren();

    //used for original WidgetBox
    EMap<QString, WidgetBoxFrame*> m_mapStrToBoxFrame;

    //used for customed WidgetBox
    EMap<QString, QTreeWidgetItem*> m_mapStrToTopItem;
    

    CategoryType categoryType() const             { return mCategoryType; }
    void setCategoryType(const CategoryType type);

#if defined(QT_PLUGIN)
        QSize sizeHint() const Q_DECL_OVERRIDE;
#endif

    int count() const { return mTreeWidget->topLevelItemCount(); }
    QWidget *page(int index) const;
    QWidget *widget(int index) const;
    QString pageTitle() const;
    QString pageTitle(int index) const;
    bool isPageExpanded() const;
    bool isPageExpanded(int index) const;
    int currentIndex() const;

public slots:
    void setCurrentIndex(int index);

    void addPage(QWidget *widget);
    void insertPage(int index, QWidget *widget);
    void removePage(int index);

    void setPageTitle(QString const &newTitle);
    void setPageExpanded(bool expanded);
    void setPageExpanded(int index, bool expanded);

    QTreeWidgetItem * addCategory(QString pageName);
protected:
    QTreeWidgetItem * insertCategory(int index, QString pageName);
    AbstractCategory *category(int index) const;
    void createContainerWidget(QTreeWidgetItem* page, QWidget *widget);
    void createCategory(QTreeWidgetItem* page, QString pageName);


protected slots:
    void onItemClicked(QTreeWidgetItem *item, int);
    void setPageExpandedProperty();

signals:
    void currentIndexChanged(int index);
    void pageTitleChanged(const QString &title);

private:
    bool checkIndex(int index) const;
    void setupWidget(QWidget *widget);
    int getPageIndex(QTreeWidgetItem *item);
    void recreateCategories();

private:
    bool eventFilter(QObject* target, QEvent* event);

    QTreeWidget *mTreeWidget;
    CategoryType mCategoryType = Button;
};

#endif
